from app.libs.error_code import  Success
from app.libs.redprint import Redprint

from flask import jsonify

from app.libs.token_auth import auth
from app.models.case import Case
from app.validators.case import CreateCaseForm,UpdateCaseForm,IdFrom,SelectFrom


api = Redprint('case')

@api.route('/create_case', methods=['POST'])
def create_case():
    form =  CreateCaseForm().validate_for_api()
    Case.add_case(form.module_name.data,form.title.data,
                  form.priority.data,form.precondition.data,
                  form.operating_steps.data,form.expected_result.data,
                  form.actual_result.data,form.test_result.data,
                  form.bug.data)

    return Success()
@api.route('/update_case', methods=['POST'])
def update_case():
    form = UpdateCaseForm().validate_for_api()
    case = Case.query.get(form.id.data)
    Case.update_case(case,form.module_name.data,form.title.data,
                  form.priority.data,form.precondition.data,
                  form.operating_steps.data,form.expected_result.data,
                  form.actual_result.data,form.test_result.data,
                  form.bug.data)
    return Success()
@api.route('/del_case', methods=['POST'])
def del_case():
    form = IdFrom().validate_for_api()
    case = Case.query.get(form.id.data)
    Case.del_case(case)
    return Success()

@api.route('/examine_case', methods=['POST'])
def examine_case():
    form = SelectFrom().validate_for_api()
    priority=form.priority.data
    if priority:
        cases=Case.query.filter(Case.priority==form.priority.data,Case.del_status==1)
    else:
        cases=Case.query.filter(Case.priority>=0,Case.del_status==1)
    case=[case for case in cases]
    return jsonify(case)




